package com.steven.im.manager;


import android.database.Cursor;
import android.util.Log;

import com.steven.im.entity.IMMsgInfo;
import com.steven.im.entity.IMMsgList;
import com.steven.im.entity.Message;

import org.litepal.LitePal;

import java.util.ArrayList;
import java.util.List;

/**
 * 操作数据库
 */
public class DBManager {
    //批量插入数据
    public static void insert(List<IMMsgInfo> imMsgInfoList) {
        if (imMsgInfoList != null && imMsgInfoList.size() > 0) {
            for (int i = 0; i < imMsgInfoList.size(); i++) {
                imMsgInfoList.get(i).save();
            }
        }
    }

    //单个插入数据(以对象的格式)
    public static void insertOne(IMMsgInfo msgInfo) {
        msgInfo.save();
    }

    //聊天信息列表查询 并按时间进行排序 查出最后一条信息
    public static List<IMMsgList> chatMsgSelect() {
        List<IMMsgList> list = new ArrayList<>();
        Cursor cursor = LitePal.findBySQL("select * from immsginfo where msgdate in(select max(msgdate) from immsginfo group by f)  order by(msgdate*1) desc");
        if (cursor != null && cursor.moveToFirst() && cursor.getCount() > 0) {
            do {
                IMMsgList imMsgList = new IMMsgList();
                String name = cursor.getString(cursor.getColumnIndex("uname"));
                String lastMsgContent = cursor.getString(cursor.getColumnIndex("content"));
                String time = cursor.getString(cursor.getColumnIndex("msgdate"));
                String imgUrl = cursor.getString(cursor.getColumnIndex("avatarurl"));
                String msgType = cursor.getString(cursor.getColumnIndex("msgtype"));
                imMsgList.setImgUrl(imgUrl);
                imMsgList.setTime(time);
                imMsgList.setName(name);
                imMsgList.setMsgType(msgType);
                imMsgList.setLastMsgContent(lastMsgContent);
                list.add(imMsgList);
            } while (cursor.moveToNext());
        }
        assert cursor != null;
        cursor.close();
        return list;
    }

    //聊天室查询信息
    public static List<Message> chatRoomMsgSelect(String f, String chatType) {
        List<Message> list = new ArrayList();
        Cursor cursor = LitePal.findBySQL("select * from immsginfo where (f=" + f + "or t=" + f + ") and chattype=" + chatType + "order by msgdate*1 desc");
        if (cursor != null && cursor.moveToFirst() && cursor.getCount() > 0) {
            do {
                Message imMsgList = new Message();
                String name = cursor.getString(cursor.getColumnIndex("uname"));
                String lastMsgContent = cursor.getString(cursor.getColumnIndex("content"));
                String time = cursor.getString(cursor.getColumnIndex("msgdate"));
                String imgUrl = cursor.getString(cursor.getColumnIndex("avatarurl"));
                String msgType = cursor.getString(cursor.getColumnIndex("msgtype"));

                list.add(imMsgList);
            } while (cursor.moveToNext());
        }
        assert cursor != null;
        cursor.close();
        return list;
    }
    //删除数据
    //批量删除数据
    //更新数据
}
